package com.tiyende.kash.loop.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.databinding.TiyendeActivityPermissionListReasonItemBinding
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：权限列表适配器
 */
class TiyendePermissionListAdapter(recyclerView: RecyclerView?) : CommonRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        val itemType = model.optString("itemType")
        return when(itemType){
            "itemTips" -> {
                R.layout.tiyende_activity_permission_list_tips_item
            }
            else -> {
                R.layout.tiyende_activity_permission_list_item
            }
        }
    }

    override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
        val itemType = model.optString("itemType")
        when (itemType){
            "itemTips" -> {//提示
            }
            else -> {
                showTiyendePermissionReasonItemView(helper,position,model)
            }
        }
    }

    /**
     * 权限声明itemView
     */
    private fun showTiyendePermissionReasonItemView(helper: CommonViewHolderHelper, position: Int, model: JSONObject){
        helper.setText(R.id.textTitle,model.optString("title"))

        val childItemLayout = helper.getView<LinearLayout>(R.id.permissionChildLayout)
        childItemLayout.removeAllViews()
        val itemList = model.optJSONArray("msgList")
        itemList?.let {
            for (i in 0..< itemList.length()){
                val item = itemList.optJSONObject(i)
                val itemView = TiyendeActivityPermissionListReasonItemBinding.inflate(LayoutInflater.from(mContext))
                val showTitle = item.optString("title")
                if (showTitle.isNotEmpty()){
                    itemView.textTitle.text = showTitle
                    itemView.textTitle.visibility = View.VISIBLE
                } else {
                    itemView.textTitle.visibility = View.GONE
                }
                itemView.textReason.text = item.optString("des")
                childItemLayout.addView(itemView.root)
            }
        }
    }
}